<?php

namespace app\controller;

use app\BaseController;
use app\model\Company;
use app\model\Group;
use app\service\CarService;
use think\exception\ValidateException;
use think\facade\Request;
use app\model\PartnerUser;

/**
 * 车辆管理
 */
class Car extends BaseController
{
    /**
     * @desc 车辆详情
     * @return void
     */
    public function info()
    {
        \request();
        $uid = Request::param('uid/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $param['id'] = Request::param('id/d', 0);
        $info = CarService::info($param);
        returnSuccess(200, '获取成功', $info);
    }

    /**
     * @desc 车辆信息
     * @return void
     */
    public function lists()
    {
        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['is_export'] = Request::param('is_export');
        $param['partner_id'] = get_partner_id($uid);
        $param['car_num'] = Request::param('car_num/s', '');
        $param['company_name'] = Request::param('company_name/s', '');
        $param['is_use'] = Request::param('is_use/d', -1);
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $info = CarService::lists($param);
        returnSuccess(200, '获取成功', $info);
    }

    //新增车辆信息
    public function add()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['car_plate_number'] = Request::param('car_plate_number/s', '');
        $param['horsepower'] = Request::param('horsepower/s', '');
        $param['brand'] = Request::param('brand/s', 0);
        $param['vehicle_weight'] = Request::param('vehicle_weight/s', '');
        $param['car_series'] = Request::param('car_series/s', '');
        $param['emission_standard'] = Request::param('emission_standard/s', 0);
        $param['rated_load'] = Request::param('rated_load/s', 0);
        $param['fuel_type'] = Request::param('fuel_type/d', 1);
        $param['factory_life'] = Request::param('factory_life/s', 0);
        $param['container_length'] = Request::param('container_length/s', 0);
        $param['mileage'] = Request::param('mileage/s', 0);
        $param['width'] = Request::param('width/s', '');
        $param['inspect_time'] = Request::param('inspect_time/s', 0);
        $param['height'] = Request::param('height/s', 0);
        $param['compulsory_date'] = Request::param('compulsory_date/s', 0);
        $param['service_company'] = Request::param('service_company/s', 0);
        $param['business_date'] = Request::param('business_date/s', 0);
        $param['buy_date'] = Request::param('buy_date/s', 0);
        $param['buy_price'] = Request::param('buy_price/s', 0);
        $param['length'] = Request::param('length/s', 0);
        $param['status'] = Request::param('status/d', 1);
        $param['owner_type'] = Request::param('owner_type/d', 0);
        $param['owner_name'] = Request::param('owner_name/s', '');

        $param['company_id'] = Request::param('company_id/d',0);
        if(!$param['company_id'])    returnFail(21009, '公司id不能为空');
        $group_id = Company::where('id',$param['company_id'])->value('group_id');
        if(!$group_id)    returnFail(21009, '公司有误');
        $param['uid'] = '1-'.$group_id.'-'.$param['company_id'];


        $logParam['ip'] = Request::ip();
        try {
            validate(\app\validate\CarValidate::class)->check([
                'car_plate_number' => $param['car_plate_number'],
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        $data = CarService::add($param, $logParam);
        if ($data == 10000) {
            returnSuccess(200, '添加成功');
        } else {
            returnFail(21101, '添加失败');
        }
    }

    //修改司机信息
    public function modify()
    {
        $uid = Request::param('uid/d', 0);
        $id = Request::param('id/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['car_plate_number'] = Request::param('car_plate_number/s', '');
        $param['horsepower'] = Request::param('horsepower/s', '');
        $param['brand'] = Request::param('brand/s', 0);
        $param['vehicle_weight'] = Request::param('vehicle_weight/s', '');
        $param['car_series'] = Request::param('car_series/s', '');
        $param['emission_standard'] = Request::param('emission_standard/s', 0);
        $param['rated_load'] = Request::param('rated_load/s', 0);
        $param['fuel_type'] = Request::param('fuel_type/d', 1);
        $param['factory_life'] = Request::param('factory_life/s', 0);
        $param['container_length'] = Request::param('container_length/s', 0);
        $param['mileage'] = Request::param('mileage/s', 0);
        $param['width'] = Request::param('width/s', '');
        $param['inspect_time'] = Request::param('inspect_time/s', 0);
        $param['height'] = Request::param('height/s', 0);
        $param['compulsory_date'] = Request::param('compulsory_date/s', 0);
        $param['service_company'] = Request::param('service_company/s', 0);
        $param['business_date'] = Request::param('business_date/s', 0);
        $param['buy_date'] = Request::param('buy_date/s', 0);
        $param['buy_price'] = Request::param('buy_price/s', 0);
        $param['length'] = Request::param('length/s', 0);
        $param['status'] = Request::param('status/d', 1);
        $param['owner_type'] = Request::param('owner_type/d', 0);
        $param['owner_name'] = Request::param('owner_name/s', '');

        $param['company_id'] = Request::param('company_id/d',0);
        if(!$param['company_id'])    returnFail(21009, '公司id不能为空');
        $group_id = Company::where('id',$param['company_id'])->value('group_id');
        if(!$group_id)    returnFail(21009, '公司有误');
        $param['uid'] = '1-'.$group_id.'-'.$param['company_id'];

        $logParam['ip'] = Request::ip();
        try {
            validate(\app\validate\CarValidate::class)->check([
                'car_plate_number' => $param['car_plate_number'],
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        $data = CarService::edit($param, $logParam, $id);
        if ($data == 10000) {
            returnSuccess(200, '修改成功');
        } else {
            returnFail(21101, '修改失败');
        }
    }

    //删除车辆信息
    public function del()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $id = Request::param('id/d', 0);
        $logParam['ip'] = Request::ip();
        $data = CarService::del($param, $logParam, $id);
        if ($data == 10000) {
            returnSuccess(200, '删除成功');
        } else {
            returnFail(21101, '添加失败');
        }
    }

    //运维数据
    public function carOper()
    {
        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['is_export'] = Request::param('is_export/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $param['car_plate_number'] = Request::param('car_plate_number/s', '');
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $info = CarService::carOper($param);
        returnSuccess(200, '获取成功', $info);
    }
}
